<?php
namespace app\common\component\lottery_1_log;

use app\common\model\Lottery1Log;
use app\common\model\UserLotteryLoading;

use app\common\component\user_wallet\UserWalletModel;
use app\common\component\user_wallet_log\UserWalletLogService;

class Lottery1LogService {
	public function makeLog($create_time, $open_time, $high_bonus, $current_bonus, $each_num_price, $num_detail = '{}') {
		$data['num'] = 0;
		$data['create_time'] = $create_time;
		$data['stop_time'] = $open_time - 120;
		$data['open_time'] = $open_time;
		$data['high_bonus'] = $high_bonus;
		$data['current_bonus'] = $current_bonus;
		$data['each_num_price'] = $each_num_price;
		$data['num_detail'] = $num_detail;

		return $this -> create($data);
	}

	public function create($data) {
		$lottery1Log = new Lottery1Log($data);

    	if($lottery1Log -> save()) {
    		return [
    			'code' => 0,
    			'message' => '玩法一日志成功!',
    			'data' => [
    				'lottery_1_log' => $lottery1Log
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '玩法一日志失败!'
		];
	}

	public function handleLottery() {
		$lastLottery1Log = Lottery1Log::order('lottery_1_log_id DESC') -> find();
		if(!$lastLottery1Log) {
			return false;
		}

		if($lastLottery1Log -> num != 0) {
			return false;
		}

		if(time() < $lastLottery1Log -> open_time) {
			return false;
		}

		$numDetail = json_decode($lastLottery1Log -> num_detail, true);
		if(empty($numDetail)) {
			$lastLottery1Log -> user_ids = '';
			$lastLottery1Log -> num = crypto_rand(getParamLottery('LOTTERY_1_MIN_NUM'), getParamLottery('LOTTERY_1_MAX_NUM'));

			$lastLottery1Log -> save();
			return true;
		}

		$userIdsStr = '';
		foreach($numDetail as $userId => $nums) {
			$userIdsStr .= $userId . ',';
		}
		$userIdsStr = rtrim($userIdsStr, ',');

		$findHighUserLotteryLoading = UserLotteryLoading::where('user_id in (' . $userIdsStr . ')') -> order('lottery_1_loading DESC') -> find();
		//$selectHighUserLotteryLoading = UserLotteryLoading::where('user_id in (' . $userIdsStr . ') AND lottery_1_loading = ' . $findHighUserLotteryLoading -> lottery_1_loading) -> select();

		$num = $numDetail[$findHighUserLotteryLoading -> user_id][0];
		$drawUserIdsStr = '';
		foreach($numDetail as $userId => $nums) {
			if(in_array($num, $nums)) {
				$drawUserIdsStr .= $userId . ',';
			}
		}
		$drawUserIdsStr = rtrim($drawUserIdsStr, ',');

		$lastLottery1Log -> num = $num;
		$lastLottery1Log -> user_ids = $drawUserIdsStr;
		$lastLottery1Log -> bonus_price = $lastLottery1Log -> current_bonus * getParamLottery('LOTTERY_1_GET_PRICE') / 100;

		$selectUserWallet = UserWalletModel::where('user_id in (' . $drawUserIdsStr . ')') -> select();
		$getBonus = $lastLottery1Log -> bonus_price / count($selectUserWallet);

		\think\Db::startTrans();
		try {

			if(!$lastLottery1Log -> save()) {
				throw new \Exception('开奖失败,lastLottery1Log, lottery_1_log_id: ' . $lastLottery1Log -> lottery_1_log_id);
			}

			if(!UserLotteryLoading::where('user_id in (' . $drawUserIdsStr . ')') -> update(['lottery_1_loading' => 0])) {
				throw new \Exception('开奖失败,UserLotteryLoading, lottery_1_log_id: ' . $lastLottery1Log -> lottery_1_log_id);
			}

			if($getBonus > 0) {
				foreach($selectUserWallet as $userWallet) {
					$userWallet -> money += $getBonus;
	
					if(!$userWallet -> save()) {
						throw new \Exception('开奖失败, userWallet, lottery_1_log_id: ' . $lastLottery1Log -> lottery_1_log_id);
					}
	
					$resultUserWalletLog = (new UserWalletLogService) -> logUserId(6, 1, $getBonus, $userWallet -> user_id, '中奖奖金', 1);
					if($resultUserWalletLog['code'] !== 0) {
						throw new \Exception($resultUserWalletLog['message']);
					}
				}
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			\think\Log::info($e -> getMessage());
		}
	}
}
?>